package com.huajiao.management_system.controller;

import com.huajiao.management_system.bean.paperRes;
import com.huajiao.management_system.bean.prove;
import com.huajiao.management_system.service.paperResService;
import com.huajiao.management_system.service.proveService;
import com.huajiao.management_system.util.PageResult;
import com.huajiao.management_system.util.Result;
import com.huajiao.management_system.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/paper")
@Api(tags = "论文成果相关接口")
public class paperResController {

    @Resource
    private paperResService paperresService;
    @Resource
    private proveService proveService;
    @PostMapping("/list")
    @ApiOperation(value = "分页查询论文成果", notes="根据一定的条件分页查询论文成果")
    @ResponseBody
    public PageResult<paperRes> list(String Time, String keyword, Integer page) {
        return paperresService.selectPage(Time,keyword,page);
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加论文成果", notes="添加论文成果")
    @ResponseBody
    public Result save(paperRes paperres, @RequestParam("files") MultipartFile[] files) {
        paperres.setCreateTime(LocalDateTime.now());
        if (paperresService.insertOne(paperres) == 0) {
            return new Result("添加失败！");
        }
        if (isUpload(paperres, files)) {
            return new Result("图片上传失败！");
        }
        return new Result("添加成功！");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改论文成果", notes="修改论文成果")
    @ResponseBody
    public Result update(paperRes paperres,String[ ] url,@RequestParam("files") MultipartFile[] files) {
        paperres.setUpdateTime(LocalDateTime.now());
        if (paperresService.updateOne(paperres) == 0) {
            return new Result("信息修改失败！");
        }
        if (isUpload(paperres, files)){
            return new Result("图片上传失败！");
        }
        if (url.length != 0) {
            if (proveService.deleteImage(url)) {
                return new Result("修改成功！");
            } else {
                return new Result("修改失败！");
            }
        }else {
            return new Result("未删除图片!");
        }
    }



    @PostMapping("/delete")
    @ApiOperation(value = "批量删除论文成果", notes="根据id数组论文删除")
    @ResponseBody
    public Result delete(Integer[] ids) {
        if (ids.length < 1) {
            return new Result("参数异常！");
        }
        if (paperresService.deleteBatch(ids)) {
            return new Result("删除成功！");
        } else {
            return new Result("删除失败");
        }
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有论文成果", notes="获取所有论文成果")
    @ResponseBody
    public List<paperRes> getAll(String Time, String keyword){
        return paperresService.getAll(Time, keyword);
    }

    private boolean isUpload(paperRes paperres, MultipartFile[] files) {
        if (files != null) {
            List<prove> list = UploadUtil.upload(files, paperres.getTypeName(),paperres.getPaperResName(), paperres.getPaperResId(), paperres.getTypeId());
            if (list != null) {
                for (prove pro : list) {
                    if (proveService.insertOne(pro) == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
